/*
* $Id: SyncChooseComPort.cpp 340 2011-10-20 08:08:01Z volok-aleksej@yandex.ru $
*
* Copyright 2009-2011 by Volokitin Aleksey
* All rights reserved
*
*/
#include "Common/include.h"
#include "SyncChooseComPort.h"
#include "SyncReadData.h"
#include "LanguageManager.h"

#define COMBO_PORT_ID 0x101

CSyncChooseComPort::CSyncChooseComPort(CWizardStep* pBack, CWnd *pParentWnd)
: CWizardWarningStep(pBack, pParentWnd), m_curSel(0){
	m_fontHeader = CCommonFunction::GetFont(_T("Arial"), 16, true, false);
	m_font = CCommonFunction::GetFont(_T("Arial"), 16, false, false);
	m_pNext = new CSyncReadData(this, pParentWnd);
}

CSyncChooseComPort::~CSyncChooseComPort(){
	m_fontHeader->DeleteObject();
	delete m_fontHeader;
	m_font->DeleteObject();
	delete m_font;
	delete m_pNext;
}

std::tstring CSyncChooseComPort::GetComPort(){
	return m_strComPort;
}

void CSyncChooseComPort::InitMain(CPoint offset){
	HWND backButton = GetDlgItem(m_pParentWnd->m_hWnd, ID_BACK);
	EnableWindow(backButton, TRUE);

	CStatic* header1 = new CStatic;
	header1->Create(CLanguageManager::GetLanguageManager()->GetString(SYNC_HEADER_ID).c_str(),
		WS_CHILD | WS_VISIBLE | SS_CENTER, CRect(offset, CSize(470, 20)), m_pParentWnd);
	header1->SetFont(m_fontHeader);
	AddWindow(header1);
	CStatic* header2 = new CStatic;
	header2->Create(CLanguageManager::GetLanguageManager()->GetString(SYNC_COM_PORT_ID).c_str(),
		WS_CHILD | WS_VISIBLE | SS_LEFT, CRect(offset + CPoint(20, 20), CSize(450, 20)), m_pParentWnd);
	header2->SetFont(m_fontHeader);
	AddWindow(header2);
	m_pComboPort = new CComboBox;
	m_pComboPort->Create(WS_CHILD | WS_TABSTOP | WS_VISIBLE | CBS_DROPDOWNLIST | WS_VSCROLL,
		CRect(offset + CPoint(20, 40), CSize(200, 100)), m_pParentWnd, COMBO_PORT_ID);
	m_pComboPort->SetFont(m_font);
	FillComPorts();
	AddWindow(m_pComboPort);
}

void CSyncChooseComPort::InitWarning(CPoint offset){
	m_pWarning = new CStatic;
	m_pWarning->Create(CLanguageManager::GetLanguageManager()->GetString(SYNC_WARNING_ID).c_str(),
		WS_CHILD | SS_CENTER, CRect(offset + CPoint(20, 80), CSize(450, 60)), m_pParentWnd);
	m_pWarning->SetFont(m_fontHeader);
	AddWindow(m_pWarning);
	Update();
}

BEGIN_MESSAGE_HANDLER(CSyncChooseComPort)
	ON_COMMAND_MSG(CBN_SELCHANGE, COMBO_PORT_ID, OnComboPortChange)
END_MESSAGE_HANDLER()

bool CSyncChooseComPort::OnComboPortChange(long param)
{
	HWND nextButton = GetDlgItem(m_pParentWnd->m_hWnd, ID_NEXT);
	m_curSel = m_pComboPort->GetCurSel();
	if(m_curSel <= 0)
		EnableWindow(nextButton, FALSE);
	else
		EnableWindow(nextButton, TRUE);
	return true;
}

CWizardStep* CSyncChooseComPort::Next(){
	CString strValue;
	m_pComboPort->GetWindowText(strValue);
	m_strComPort = strValue.GetString();
	CloseWindows();
	return m_pNext;
}

CWizardStep* CSyncChooseComPort::Back(){
	CloseWindows();
	return m_pBack;
}

void CSyncChooseComPort::FillComPorts()
{
	m_pComboPort->AddString(CLanguageManager::GetLanguageManager()->GetString(DEFAULT_COM_PORT_ID).c_str());
	m_pComboPort->SetCurSel(0);
	HKEY hKey;
	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
					0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
		return;
	int  NumVal = 0;
	TCHAR pName[40];
	TCHAR pValue[40];
	DWORD sizeName, sizeValue, type;
	sizeName = sizeValue = 40;
	DWORD res = RegEnumValue(hKey, NumVal++, pName, &sizeName,
		NULL, &type, (LPBYTE)pValue, &sizeValue);
	while(!res || res == ERROR_MORE_DATA){
		int index = m_pComboPort->AddString(pValue);
		if(m_strComPort == pValue)
		{
			m_pComboPort->SetCurSel(index);
			m_curSel = index;
		}
		sizeName = sizeValue = 40;
		res = RegEnumValue(hKey, NumVal++, pName, &sizeName,
							NULL, &type, (LPBYTE)pValue, &sizeValue);
	}
	RegCloseKey(hKey);
	OnComboPortChange(0);
}

void CSyncChooseComPort::UpdateComPorts()
{
	std::vector<std::tstring> aPorts;
	HKEY hKey;
	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
					0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
		return;
	int  NumVal = 0;
	TCHAR pName[40];
	TCHAR pValue[40];
	DWORD sizeName, sizeValue, type;
	sizeName = sizeValue = 40;
	DWORD res = RegEnumValue(hKey, NumVal++, pName, &sizeName,
		NULL, &type, (LPBYTE)pValue, &sizeValue);
	while(!res || res == ERROR_MORE_DATA){
		aPorts.push_back(pValue);
		if(m_pComboPort->FindString(0, pValue) == CB_ERR)
			m_pComboPort->AddString(pValue);
		sizeName = sizeValue = 40;
		res = RegEnumValue(hKey, NumVal++, pName, &sizeName,
							NULL, &type, (LPBYTE)pValue, &sizeValue);
	}
	RegCloseKey(hKey);

	for(int i = 1; i < m_pComboPort->GetCount(); i++){
		CString strValue;
		m_pComboPort->GetLBText(i, strValue);
		std::vector<std::tstring>::iterator it = 
			std::find(aPorts.begin(), aPorts.end(), strValue.GetString());
		if(it == aPorts.end()){
			m_pComboPort->DeleteString(i);
			i--;
		}
	}
}

void CSyncChooseComPort::Update()
{
	UpdateComPorts();
	if(m_curSel > 0)
		__super::Update();
}